(function() {
'use strict';

angular
	.module('votingBackend')
	.controller('LoginController', ['$scope', '$rootScope', '$http', '$location', '$cookies', '$timeout', 'toastr', 'userDataService', 'AUTH_EVENTS', 'AuthService', '$state', LoginController]);


function LoginController($scope, $rootScope, $http, $location, $cookies, $timeout, toastr, userDataService, AUTH_EVENTS, AuthService, $state) {

	var _init = function(){

	}

	_init();

	// var baseUrl = 'http://115.29.194.90:1234/'
	var baseUrl = 'http://localhost:1235/'


	$scope.submitSignupForm = function(){

		// Set the loading state (i.e. show loading spinner)
		$scope.signupForm.loading = true;

		// Submit request to Sails.

		// for production
		//$http.post('http://115.29.194.90:1234/user/create', {
		// for development
		$http.post(baseUrl + 'user/create', {
			name: $scope.signupForm.name,
			email: $scope.signupForm.email,
			password: $scope.signupForm.password,
			//passwordConfirm: $scope.signupForm.passwordConfirm
		})
		.then(function onSuccess(sailsResponse){
			window.location = '/#/dashboard';
			$rootScope.currentUser = sailsResponse.data.id;
			$scope.setCurrentUser(sailsResponse.data.id);
			$cookies.put('userToken', $rootScope.currentUser);


			//console.log($cookies.get('userToken'))
		})
		.catch(function onError(sailsResponse){
			console.log('error')
			//console.log(sailsResponse);

		// Handle known error type(s).
		// If using sails-disk adpater -- Handle Duplicate Key
		//var emailAddressAlreadyInUse = sailsResponse.status == 409;
			if (sailsResponse.status == 409){
				toastr.error('That email address has already been taken, please try again.', 'Error', {
					closeButton: true
				});
				return;
			}

			if (sailsResponse.data.error == 'duplicate entry') {
				toastr.error('That email address has already been taken, please try again.', 'Error', {
					closeButton: true
				});
				return;
			}

			if (sailsResponse.status == 500){
				toastr.error('An unknown server error has occured. Please try again later.', 'Error', {
					closeButton: true
				});
				return;
			}

			if (sailsResponse.status == 0){
				toastr.error("We're sorry, but we seem to be having connection issues at the moment. Please try again later.", "Error", {
					closeButton: true
				});
				return;
			}

		})
		.finally(function eitherWay(){
			$scope.signupForm.loading = false;
		})
	}

	$scope.credentials = {
		email: '',
		password: ''
	};

	$scope.submitLoginForm = function (credentials) {
		AuthService.login(credentials).then(function (user) {
			console.log('login success');
			$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
			$scope.setCurrentUser(user);
			window.location = '/#/dashboard';
			toastr.success('Login Successful','Success', {
				closeButton: true
			});
		}, function (error) {
			console.log('login failed')
			if (error.status == 500){
				toastr.error('An unknown server error has occured. Please try again later.', 'Error', {
					closeButton: true
				});
				return;
			}
			if (error.status == 404){
				toastr.error("You've entered an incorrect email or password. Please try again.", 'Error', {
					closeButton: true
				});
				return;
			}
			//console.log(error)
			//$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
		});
	};

/*
	$scope.submitLoginForm = function (){

    // Set the loading state (i.e. show loading spinner)
    $scope.loginForm.loading = true;

    // Submit request to Sails.
    $http.put('http://localhost:1337/login', {
      email: $scope.loginForm.email,
      password: $scope.loginForm.password
    })
    .then(function onSuccess (sailsResponse){
      // Refresh the page now that we've been logged in.
      window.location = '/#/dashboard';
      console.log(sailsResponse.data.id)
      //$rootScope.currentUser = sailsResponse.data.id
    })
    .catch(function onError(sailsResponse) {

      // Handle known error type(s).
      // Invalid username / password combination.
      if (sailsResponse.status === 400 || 404) {
        // $scope.loginForm.topLevelErrorMessage = 'Invalid email/password combination.';
        //
        toastr.error('Invalid email/password combination.', 'Error', {
          closeButton: true
        });
        return;
      }

		toastr.error('An unexpected error occurred, please try again.', 'Error', {
			closeButton: true
		});
		return;

    })
    .finally(function eitherWay(){
      $scope.loginForm.loading = false;
    });
  };

*/
}


})();